<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>long.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_long.js.html">long.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Defines a Long class for representing a 64-bit two&#39;s-complement
<a name="line17"></a> * integer value, which faithfully simulates the behavior of a Java &quot;long&quot;. This
<a name="line18"></a> * implementation is derived from LongLib in GWT.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.math.Long&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Constructs a 64-bit two&#39;s-complement integer, given its low and high 32-bit
<a name="line28"></a> * values as *signed* integers.  See the from* functions below for more
<a name="line29"></a> * convenient ways of constructing Longs.
<a name="line30"></a> *
<a name="line31"></a> * The internal representation of a long is the two given signed, 32-bit values.
<a name="line32"></a> * We use 32-bit pieces because these are the size of integers on which
<a name="line33"></a> * Javascript performs bit-operations.  For operations like addition and
<a name="line34"></a> * multiplication, we split each number into 16-bit pieces, which can easily be
<a name="line35"></a> * multiplied within Javascript&#39;s floating-point representation without overflow
<a name="line36"></a> * or change in sign.
<a name="line37"></a> *
<a name="line38"></a> * In the algorithms below, we frequently reduce the negative case to the
<a name="line39"></a> * positive case by negating the input(s) and then post-processing the result.
<a name="line40"></a> * Note that we must ALWAYS check specially whether those values are MIN_VALUE
<a name="line41"></a> * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
<a name="line42"></a> * a positive number, it overflows back into a negative).  Not handling this
<a name="line43"></a> * case would often result in infinite recursion.
<a name="line44"></a> *
<a name="line45"></a> * @param {number} low  The low (signed) 32 bits of the long.
<a name="line46"></a> * @param {number} high  The high (signed) 32 bits of the long.
<a name="line47"></a> * @constructor
<a name="line48"></a> */
<a name="line49"></a>goog.math.Long = function(low, high) {
<a name="line50"></a>  /**
<a name="line51"></a>   * @type {number}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.low_ = low | 0;  // force into 32 signed bits.
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * @type {number}
<a name="line58"></a>   * @private
<a name="line59"></a>   */
<a name="line60"></a>  this.high_ = high | 0;  // force into 32 signed bits.
<a name="line61"></a>};
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
<a name="line65"></a>// from* methods on which they depend.
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * A cache of the Long representations of small integer values.
<a name="line70"></a> * @type {!Object}
<a name="line71"></a> * @private
<a name="line72"></a> */
<a name="line73"></a>goog.math.Long.IntCache_ = {};
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Returns a Long representing the given (32-bit) integer value.
<a name="line78"></a> * @param {number} value The 32-bit integer in question.
<a name="line79"></a> * @return {!goog.math.Long} The corresponding Long value.
<a name="line80"></a> */
<a name="line81"></a>goog.math.Long.fromInt = function(value) {
<a name="line82"></a>  if (-128 &lt;= value &amp;&amp; value &lt; 128) {
<a name="line83"></a>    var cachedObj = goog.math.Long.IntCache_[value];
<a name="line84"></a>    if (cachedObj) {
<a name="line85"></a>      return cachedObj;
<a name="line86"></a>    }
<a name="line87"></a>  }
<a name="line88"></a>
<a name="line89"></a>  var obj = new goog.math.Long(value | 0, value &lt; 0 ? -1 : 0);
<a name="line90"></a>  if (-128 &lt;= value &amp;&amp; value &lt; 128) {
<a name="line91"></a>    goog.math.Long.IntCache_[value] = obj;
<a name="line92"></a>  }
<a name="line93"></a>  return obj;
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Returns a Long representing the given value, provided that it is a finite
<a name="line99"></a> * number.  Otherwise, zero is returned.
<a name="line100"></a> * @param {number} value The number in question.
<a name="line101"></a> * @return {!goog.math.Long} The corresponding Long value.
<a name="line102"></a> */
<a name="line103"></a>goog.math.Long.fromNumber = function(value) {
<a name="line104"></a>  if (isNaN(value) || !isFinite(value)) {
<a name="line105"></a>    return goog.math.Long.ZERO;
<a name="line106"></a>  } else if (value &lt;= -goog.math.Long.TWO_PWR_63_DBL_) {
<a name="line107"></a>    return goog.math.Long.MIN_VALUE;
<a name="line108"></a>  } else if (value + 1 &gt;= goog.math.Long.TWO_PWR_63_DBL_) {
<a name="line109"></a>    return goog.math.Long.MAX_VALUE;
<a name="line110"></a>  } else if (value &lt; 0) {
<a name="line111"></a>    return goog.math.Long.fromNumber(-value).negate();
<a name="line112"></a>  } else {
<a name="line113"></a>    return new goog.math.Long(
<a name="line114"></a>        (value % goog.math.Long.TWO_PWR_32_DBL_) | 0,
<a name="line115"></a>        (value / goog.math.Long.TWO_PWR_32_DBL_) | 0);
<a name="line116"></a>  }
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Returns a Long representing the 64-bit integer that comes by concatenating
<a name="line122"></a> * the given high and low bits.  Each is assumed to use 32 bits.
<a name="line123"></a> * @param {number} lowBits The low 32-bits.
<a name="line124"></a> * @param {number} highBits The high 32-bits.
<a name="line125"></a> * @return {!goog.math.Long} The corresponding Long value.
<a name="line126"></a> */
<a name="line127"></a>goog.math.Long.fromBits = function(lowBits, highBits) {
<a name="line128"></a>  return new goog.math.Long(lowBits, highBits);
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Returns a Long representation of the given string, written using the given
<a name="line134"></a> * radix.
<a name="line135"></a> * @param {string} str The textual representation of the Long.
<a name="line136"></a> * @param {number=} opt_radix The radix in which the text is written.
<a name="line137"></a> * @return {!goog.math.Long} The corresponding Long value.
<a name="line138"></a> */
<a name="line139"></a>goog.math.Long.fromString = function(str, opt_radix) {
<a name="line140"></a>  if (str.length == 0) {
<a name="line141"></a>    throw Error(&#39;number format error: empty string&#39;);
<a name="line142"></a>  }
<a name="line143"></a>
<a name="line144"></a>  var radix = opt_radix || 10;
<a name="line145"></a>  if (radix &lt; 2 || 36 &lt; radix) {
<a name="line146"></a>    throw Error(&#39;radix out of range: &#39; + radix);
<a name="line147"></a>  }
<a name="line148"></a>
<a name="line149"></a>  if (str.charAt(0) == &#39;-&#39;) {
<a name="line150"></a>    return goog.math.Long.fromString(str.substring(1), radix).negate();
<a name="line151"></a>  } else if (str.indexOf(&#39;-&#39;) &gt;= 0) {
<a name="line152"></a>    throw Error(&#39;number format error: interior &quot;-&quot; character: &#39; + str);
<a name="line153"></a>  }
<a name="line154"></a>
<a name="line155"></a>  // Do several (8) digits each time through the loop, so as to
<a name="line156"></a>  // minimize the calls to the very expensive emulated div.
<a name="line157"></a>  var radixToPower = goog.math.Long.fromNumber(Math.pow(radix, 8));
<a name="line158"></a>
<a name="line159"></a>  var result = goog.math.Long.ZERO;
<a name="line160"></a>  for (var i = 0; i &lt; str.length; i += 8) {
<a name="line161"></a>    var size = Math.min(8, str.length - i);
<a name="line162"></a>    var value = parseInt(str.substring(i, i + size), radix);
<a name="line163"></a>    if (size &lt; 8) {
<a name="line164"></a>      var power = goog.math.Long.fromNumber(Math.pow(radix, size));
<a name="line165"></a>      result = result.multiply(power).add(goog.math.Long.fromNumber(value));
<a name="line166"></a>    } else {
<a name="line167"></a>      result = result.multiply(radixToPower);
<a name="line168"></a>      result = result.add(goog.math.Long.fromNumber(value));
<a name="line169"></a>    }
<a name="line170"></a>  }
<a name="line171"></a>  return result;
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>// NOTE: the compiler should inline these constant values below and then remove
<a name="line176"></a>// these variables, so there should be no runtime penalty for these.
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Number used repeated below in calculations.  This must appear before the
<a name="line181"></a> * first call to any from* function below.
<a name="line182"></a> * @type {number}
<a name="line183"></a> * @private
<a name="line184"></a> */
<a name="line185"></a>goog.math.Long.TWO_PWR_16_DBL_ = 1 &lt;&lt; 16;
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * @type {number}
<a name="line190"></a> * @private
<a name="line191"></a> */
<a name="line192"></a>goog.math.Long.TWO_PWR_24_DBL_ = 1 &lt;&lt; 24;
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * @type {number}
<a name="line197"></a> * @private
<a name="line198"></a> */
<a name="line199"></a>goog.math.Long.TWO_PWR_32_DBL_ =
<a name="line200"></a>    goog.math.Long.TWO_PWR_16_DBL_ * goog.math.Long.TWO_PWR_16_DBL_;
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * @type {number}
<a name="line205"></a> * @private
<a name="line206"></a> */
<a name="line207"></a>goog.math.Long.TWO_PWR_31_DBL_ =
<a name="line208"></a>    goog.math.Long.TWO_PWR_32_DBL_ / 2;
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>/**
<a name="line212"></a> * @type {number}
<a name="line213"></a> * @private
<a name="line214"></a> */
<a name="line215"></a>goog.math.Long.TWO_PWR_48_DBL_ =
<a name="line216"></a>    goog.math.Long.TWO_PWR_32_DBL_ * goog.math.Long.TWO_PWR_16_DBL_;
<a name="line217"></a>
<a name="line218"></a>
<a name="line219"></a>/**
<a name="line220"></a> * @type {number}
<a name="line221"></a> * @private
<a name="line222"></a> */
<a name="line223"></a>goog.math.Long.TWO_PWR_64_DBL_ =
<a name="line224"></a>    goog.math.Long.TWO_PWR_32_DBL_ * goog.math.Long.TWO_PWR_32_DBL_;
<a name="line225"></a>
<a name="line226"></a>
<a name="line227"></a>/**
<a name="line228"></a> * @type {number}
<a name="line229"></a> * @private
<a name="line230"></a> */
<a name="line231"></a>goog.math.Long.TWO_PWR_63_DBL_ =
<a name="line232"></a>    goog.math.Long.TWO_PWR_64_DBL_ / 2;
<a name="line233"></a>
<a name="line234"></a>
<a name="line235"></a>/** @type {!goog.math.Long} */
<a name="line236"></a>goog.math.Long.ZERO = goog.math.Long.fromInt(0);
<a name="line237"></a>
<a name="line238"></a>
<a name="line239"></a>/** @type {!goog.math.Long} */
<a name="line240"></a>goog.math.Long.ONE = goog.math.Long.fromInt(1);
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>/** @type {!goog.math.Long} */
<a name="line244"></a>goog.math.Long.NEG_ONE = goog.math.Long.fromInt(-1);
<a name="line245"></a>
<a name="line246"></a>
<a name="line247"></a>/** @type {!goog.math.Long} */
<a name="line248"></a>goog.math.Long.MAX_VALUE =
<a name="line249"></a>    goog.math.Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0);
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>/** @type {!goog.math.Long} */
<a name="line253"></a>goog.math.Long.MIN_VALUE = goog.math.Long.fromBits(0, 0x80000000 | 0);
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * @type {!goog.math.Long}
<a name="line258"></a> * @private
<a name="line259"></a> */
<a name="line260"></a>goog.math.Long.TWO_PWR_24_ = goog.math.Long.fromInt(1 &lt;&lt; 24);
<a name="line261"></a>
<a name="line262"></a>
<a name="line263"></a>/** @return {number} The value, assuming it is a 32-bit integer. */
<a name="line264"></a>goog.math.Long.prototype.toInt = function() {
<a name="line265"></a>  return this.low_;
<a name="line266"></a>};
<a name="line267"></a>
<a name="line268"></a>
<a name="line269"></a>/** @return {number} The closest floating-point representation to this value. */
<a name="line270"></a>goog.math.Long.prototype.toNumber = function() {
<a name="line271"></a>  return this.high_ * goog.math.Long.TWO_PWR_32_DBL_ +
<a name="line272"></a>         this.getLowBitsUnsigned();
<a name="line273"></a>};
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * @param {number=} opt_radix The radix in which the text should be written.
<a name="line278"></a> * @return {string} The textual representation of this value.
<a name="line279"></a> * @override
<a name="line280"></a> */
<a name="line281"></a>goog.math.Long.prototype.toString = function(opt_radix) {
<a name="line282"></a>  var radix = opt_radix || 10;
<a name="line283"></a>  if (radix &lt; 2 || 36 &lt; radix) {
<a name="line284"></a>    throw Error(&#39;radix out of range: &#39; + radix);
<a name="line285"></a>  }
<a name="line286"></a>
<a name="line287"></a>  if (this.isZero()) {
<a name="line288"></a>    return &#39;0&#39;;
<a name="line289"></a>  }
<a name="line290"></a>
<a name="line291"></a>  if (this.isNegative()) {
<a name="line292"></a>    if (this.equals(goog.math.Long.MIN_VALUE)) {
<a name="line293"></a>      // We need to change the Long value before it can be negated, so we remove
<a name="line294"></a>      // the bottom-most digit in this base and then recurse to do the rest.
<a name="line295"></a>      var radixLong = goog.math.Long.fromNumber(radix);
<a name="line296"></a>      var div = this.div(radixLong);
<a name="line297"></a>      var rem = div.multiply(radixLong).subtract(this);
<a name="line298"></a>      return div.toString(radix) + rem.toInt().toString(radix);
<a name="line299"></a>    } else {
<a name="line300"></a>      return &#39;-&#39; + this.negate().toString(radix);
<a name="line301"></a>    }
<a name="line302"></a>  }
<a name="line303"></a>
<a name="line304"></a>  // Do several (6) digits each time through the loop, so as to
<a name="line305"></a>  // minimize the calls to the very expensive emulated div.
<a name="line306"></a>  var radixToPower = goog.math.Long.fromNumber(Math.pow(radix, 6));
<a name="line307"></a>
<a name="line308"></a>  var rem = this;
<a name="line309"></a>  var result = &#39;&#39;;
<a name="line310"></a>  while (true) {
<a name="line311"></a>    var remDiv = rem.div(radixToPower);
<a name="line312"></a>    var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
<a name="line313"></a>    var digits = intval.toString(radix);
<a name="line314"></a>
<a name="line315"></a>    rem = remDiv;
<a name="line316"></a>    if (rem.isZero()) {
<a name="line317"></a>      return digits + result;
<a name="line318"></a>    } else {
<a name="line319"></a>      while (digits.length &lt; 6) {
<a name="line320"></a>        digits = &#39;0&#39; + digits;
<a name="line321"></a>      }
<a name="line322"></a>      result = &#39;&#39; + digits + result;
<a name="line323"></a>    }
<a name="line324"></a>  }
<a name="line325"></a>};
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>/** @return {number} The high 32-bits as a signed value. */
<a name="line329"></a>goog.math.Long.prototype.getHighBits = function() {
<a name="line330"></a>  return this.high_;
<a name="line331"></a>};
<a name="line332"></a>
<a name="line333"></a>
<a name="line334"></a>/** @return {number} The low 32-bits as a signed value. */
<a name="line335"></a>goog.math.Long.prototype.getLowBits = function() {
<a name="line336"></a>  return this.low_;
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>/** @return {number} The low 32-bits as an unsigned value. */
<a name="line341"></a>goog.math.Long.prototype.getLowBitsUnsigned = function() {
<a name="line342"></a>  return (this.low_ &gt;= 0) ?
<a name="line343"></a>      this.low_ : goog.math.Long.TWO_PWR_32_DBL_ + this.low_;
<a name="line344"></a>};
<a name="line345"></a>
<a name="line346"></a>
<a name="line347"></a>/**
<a name="line348"></a> * @return {number} Returns the number of bits needed to represent the absolute
<a name="line349"></a> *     value of this Long.
<a name="line350"></a> */
<a name="line351"></a>goog.math.Long.prototype.getNumBitsAbs = function() {
<a name="line352"></a>  if (this.isNegative()) {
<a name="line353"></a>    if (this.equals(goog.math.Long.MIN_VALUE)) {
<a name="line354"></a>      return 64;
<a name="line355"></a>    } else {
<a name="line356"></a>      return this.negate().getNumBitsAbs();
<a name="line357"></a>    }
<a name="line358"></a>  } else {
<a name="line359"></a>    var val = this.high_ != 0 ? this.high_ : this.low_;
<a name="line360"></a>    for (var bit = 31; bit &gt; 0; bit--) {
<a name="line361"></a>      if ((val &amp; (1 &lt;&lt; bit)) != 0) {
<a name="line362"></a>        break;
<a name="line363"></a>      }
<a name="line364"></a>    }
<a name="line365"></a>    return this.high_ != 0 ? bit + 33 : bit + 1;
<a name="line366"></a>  }
<a name="line367"></a>};
<a name="line368"></a>
<a name="line369"></a>
<a name="line370"></a>/** @return {boolean} Whether this value is zero. */
<a name="line371"></a>goog.math.Long.prototype.isZero = function() {
<a name="line372"></a>  return this.high_ == 0 &amp;&amp; this.low_ == 0;
<a name="line373"></a>};
<a name="line374"></a>
<a name="line375"></a>
<a name="line376"></a>/** @return {boolean} Whether this value is negative. */
<a name="line377"></a>goog.math.Long.prototype.isNegative = function() {
<a name="line378"></a>  return this.high_ &lt; 0;
<a name="line379"></a>};
<a name="line380"></a>
<a name="line381"></a>
<a name="line382"></a>/** @return {boolean} Whether this value is odd. */
<a name="line383"></a>goog.math.Long.prototype.isOdd = function() {
<a name="line384"></a>  return (this.low_ &amp; 1) == 1;
<a name="line385"></a>};
<a name="line386"></a>
<a name="line387"></a>
<a name="line388"></a>/**
<a name="line389"></a> * @param {goog.math.Long} other Long to compare against.
<a name="line390"></a> * @return {boolean} Whether this Long equals the other.
<a name="line391"></a> */
<a name="line392"></a>goog.math.Long.prototype.equals = function(other) {
<a name="line393"></a>  return (this.high_ == other.high_) &amp;&amp; (this.low_ == other.low_);
<a name="line394"></a>};
<a name="line395"></a>
<a name="line396"></a>
<a name="line397"></a>/**
<a name="line398"></a> * @param {goog.math.Long} other Long to compare against.
<a name="line399"></a> * @return {boolean} Whether this Long does not equal the other.
<a name="line400"></a> */
<a name="line401"></a>goog.math.Long.prototype.notEquals = function(other) {
<a name="line402"></a>  return (this.high_ != other.high_) || (this.low_ != other.low_);
<a name="line403"></a>};
<a name="line404"></a>
<a name="line405"></a>
<a name="line406"></a>/**
<a name="line407"></a> * @param {goog.math.Long} other Long to compare against.
<a name="line408"></a> * @return {boolean} Whether this Long is less than the other.
<a name="line409"></a> */
<a name="line410"></a>goog.math.Long.prototype.lessThan = function(other) {
<a name="line411"></a>  return this.compare(other) &lt; 0;
<a name="line412"></a>};
<a name="line413"></a>
<a name="line414"></a>
<a name="line415"></a>/**
<a name="line416"></a> * @param {goog.math.Long} other Long to compare against.
<a name="line417"></a> * @return {boolean} Whether this Long is less than or equal to the other.
<a name="line418"></a> */
<a name="line419"></a>goog.math.Long.prototype.lessThanOrEqual = function(other) {
<a name="line420"></a>  return this.compare(other) &lt;= 0;
<a name="line421"></a>};
<a name="line422"></a>
<a name="line423"></a>
<a name="line424"></a>/**
<a name="line425"></a> * @param {goog.math.Long} other Long to compare against.
<a name="line426"></a> * @return {boolean} Whether this Long is greater than the other.
<a name="line427"></a> */
<a name="line428"></a>goog.math.Long.prototype.greaterThan = function(other) {
<a name="line429"></a>  return this.compare(other) &gt; 0;
<a name="line430"></a>};
<a name="line431"></a>
<a name="line432"></a>
<a name="line433"></a>/**
<a name="line434"></a> * @param {goog.math.Long} other Long to compare against.
<a name="line435"></a> * @return {boolean} Whether this Long is greater than or equal to the other.
<a name="line436"></a> */
<a name="line437"></a>goog.math.Long.prototype.greaterThanOrEqual = function(other) {
<a name="line438"></a>  return this.compare(other) &gt;= 0;
<a name="line439"></a>};
<a name="line440"></a>
<a name="line441"></a>
<a name="line442"></a>/**
<a name="line443"></a> * Compares this Long with the given one.
<a name="line444"></a> * @param {goog.math.Long} other Long to compare against.
<a name="line445"></a> * @return {number} 0 if they are the same, 1 if the this is greater, and -1
<a name="line446"></a> *     if the given one is greater.
<a name="line447"></a> */
<a name="line448"></a>goog.math.Long.prototype.compare = function(other) {
<a name="line449"></a>  if (this.equals(other)) {
<a name="line450"></a>    return 0;
<a name="line451"></a>  }
<a name="line452"></a>
<a name="line453"></a>  var thisNeg = this.isNegative();
<a name="line454"></a>  var otherNeg = other.isNegative();
<a name="line455"></a>  if (thisNeg &amp;&amp; !otherNeg) {
<a name="line456"></a>    return -1;
<a name="line457"></a>  }
<a name="line458"></a>  if (!thisNeg &amp;&amp; otherNeg) {
<a name="line459"></a>    return 1;
<a name="line460"></a>  }
<a name="line461"></a>
<a name="line462"></a>  // at this point, the signs are the same, so subtraction will not overflow
<a name="line463"></a>  if (this.subtract(other).isNegative()) {
<a name="line464"></a>    return -1;
<a name="line465"></a>  } else {
<a name="line466"></a>    return 1;
<a name="line467"></a>  }
<a name="line468"></a>};
<a name="line469"></a>
<a name="line470"></a>
<a name="line471"></a>/** @return {!goog.math.Long} The negation of this value. */
<a name="line472"></a>goog.math.Long.prototype.negate = function() {
<a name="line473"></a>  if (this.equals(goog.math.Long.MIN_VALUE)) {
<a name="line474"></a>    return goog.math.Long.MIN_VALUE;
<a name="line475"></a>  } else {
<a name="line476"></a>    return this.not().add(goog.math.Long.ONE);
<a name="line477"></a>  }
<a name="line478"></a>};
<a name="line479"></a>
<a name="line480"></a>
<a name="line481"></a>/**
<a name="line482"></a> * Returns the sum of this and the given Long.
<a name="line483"></a> * @param {goog.math.Long} other Long to add to this one.
<a name="line484"></a> * @return {!goog.math.Long} The sum of this and the given Long.
<a name="line485"></a> */
<a name="line486"></a>goog.math.Long.prototype.add = function(other) {
<a name="line487"></a>  // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
<a name="line488"></a>
<a name="line489"></a>  var a48 = this.high_ &gt;&gt;&gt; 16;
<a name="line490"></a>  var a32 = this.high_ &amp; 0xFFFF;
<a name="line491"></a>  var a16 = this.low_ &gt;&gt;&gt; 16;
<a name="line492"></a>  var a00 = this.low_ &amp; 0xFFFF;
<a name="line493"></a>
<a name="line494"></a>  var b48 = other.high_ &gt;&gt;&gt; 16;
<a name="line495"></a>  var b32 = other.high_ &amp; 0xFFFF;
<a name="line496"></a>  var b16 = other.low_ &gt;&gt;&gt; 16;
<a name="line497"></a>  var b00 = other.low_ &amp; 0xFFFF;
<a name="line498"></a>
<a name="line499"></a>  var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
<a name="line500"></a>  c00 += a00 + b00;
<a name="line501"></a>  c16 += c00 &gt;&gt;&gt; 16;
<a name="line502"></a>  c00 &amp;= 0xFFFF;
<a name="line503"></a>  c16 += a16 + b16;
<a name="line504"></a>  c32 += c16 &gt;&gt;&gt; 16;
<a name="line505"></a>  c16 &amp;= 0xFFFF;
<a name="line506"></a>  c32 += a32 + b32;
<a name="line507"></a>  c48 += c32 &gt;&gt;&gt; 16;
<a name="line508"></a>  c32 &amp;= 0xFFFF;
<a name="line509"></a>  c48 += a48 + b48;
<a name="line510"></a>  c48 &amp;= 0xFFFF;
<a name="line511"></a>  return goog.math.Long.fromBits((c16 &lt;&lt; 16) | c00, (c48 &lt;&lt; 16) | c32);
<a name="line512"></a>};
<a name="line513"></a>
<a name="line514"></a>
<a name="line515"></a>/**
<a name="line516"></a> * Returns the difference of this and the given Long.
<a name="line517"></a> * @param {goog.math.Long} other Long to subtract from this.
<a name="line518"></a> * @return {!goog.math.Long} The difference of this and the given Long.
<a name="line519"></a> */
<a name="line520"></a>goog.math.Long.prototype.subtract = function(other) {
<a name="line521"></a>  return this.add(other.negate());
<a name="line522"></a>};
<a name="line523"></a>
<a name="line524"></a>
<a name="line525"></a>/**
<a name="line526"></a> * Returns the product of this and the given long.
<a name="line527"></a> * @param {goog.math.Long} other Long to multiply with this.
<a name="line528"></a> * @return {!goog.math.Long} The product of this and the other.
<a name="line529"></a> */
<a name="line530"></a>goog.math.Long.prototype.multiply = function(other) {
<a name="line531"></a>  if (this.isZero()) {
<a name="line532"></a>    return goog.math.Long.ZERO;
<a name="line533"></a>  } else if (other.isZero()) {
<a name="line534"></a>    return goog.math.Long.ZERO;
<a name="line535"></a>  }
<a name="line536"></a>
<a name="line537"></a>  if (this.equals(goog.math.Long.MIN_VALUE)) {
<a name="line538"></a>    return other.isOdd() ? goog.math.Long.MIN_VALUE : goog.math.Long.ZERO;
<a name="line539"></a>  } else if (other.equals(goog.math.Long.MIN_VALUE)) {
<a name="line540"></a>    return this.isOdd() ? goog.math.Long.MIN_VALUE : goog.math.Long.ZERO;
<a name="line541"></a>  }
<a name="line542"></a>
<a name="line543"></a>  if (this.isNegative()) {
<a name="line544"></a>    if (other.isNegative()) {
<a name="line545"></a>      return this.negate().multiply(other.negate());
<a name="line546"></a>    } else {
<a name="line547"></a>      return this.negate().multiply(other).negate();
<a name="line548"></a>    }
<a name="line549"></a>  } else if (other.isNegative()) {
<a name="line550"></a>    return this.multiply(other.negate()).negate();
<a name="line551"></a>  }
<a name="line552"></a>
<a name="line553"></a>  // If both longs are small, use float multiplication
<a name="line554"></a>  if (this.lessThan(goog.math.Long.TWO_PWR_24_) &amp;&amp;
<a name="line555"></a>      other.lessThan(goog.math.Long.TWO_PWR_24_)) {
<a name="line556"></a>    return goog.math.Long.fromNumber(this.toNumber() * other.toNumber());
<a name="line557"></a>  }
<a name="line558"></a>
<a name="line559"></a>  // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
<a name="line560"></a>  // We can skip products that would overflow.
<a name="line561"></a>
<a name="line562"></a>  var a48 = this.high_ &gt;&gt;&gt; 16;
<a name="line563"></a>  var a32 = this.high_ &amp; 0xFFFF;
<a name="line564"></a>  var a16 = this.low_ &gt;&gt;&gt; 16;
<a name="line565"></a>  var a00 = this.low_ &amp; 0xFFFF;
<a name="line566"></a>
<a name="line567"></a>  var b48 = other.high_ &gt;&gt;&gt; 16;
<a name="line568"></a>  var b32 = other.high_ &amp; 0xFFFF;
<a name="line569"></a>  var b16 = other.low_ &gt;&gt;&gt; 16;
<a name="line570"></a>  var b00 = other.low_ &amp; 0xFFFF;
<a name="line571"></a>
<a name="line572"></a>  var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
<a name="line573"></a>  c00 += a00 * b00;
<a name="line574"></a>  c16 += c00 &gt;&gt;&gt; 16;
<a name="line575"></a>  c00 &amp;= 0xFFFF;
<a name="line576"></a>  c16 += a16 * b00;
<a name="line577"></a>  c32 += c16 &gt;&gt;&gt; 16;
<a name="line578"></a>  c16 &amp;= 0xFFFF;
<a name="line579"></a>  c16 += a00 * b16;
<a name="line580"></a>  c32 += c16 &gt;&gt;&gt; 16;
<a name="line581"></a>  c16 &amp;= 0xFFFF;
<a name="line582"></a>  c32 += a32 * b00;
<a name="line583"></a>  c48 += c32 &gt;&gt;&gt; 16;
<a name="line584"></a>  c32 &amp;= 0xFFFF;
<a name="line585"></a>  c32 += a16 * b16;
<a name="line586"></a>  c48 += c32 &gt;&gt;&gt; 16;
<a name="line587"></a>  c32 &amp;= 0xFFFF;
<a name="line588"></a>  c32 += a00 * b32;
<a name="line589"></a>  c48 += c32 &gt;&gt;&gt; 16;
<a name="line590"></a>  c32 &amp;= 0xFFFF;
<a name="line591"></a>  c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
<a name="line592"></a>  c48 &amp;= 0xFFFF;
<a name="line593"></a>  return goog.math.Long.fromBits((c16 &lt;&lt; 16) | c00, (c48 &lt;&lt; 16) | c32);
<a name="line594"></a>};
<a name="line595"></a>
<a name="line596"></a>
<a name="line597"></a>/**
<a name="line598"></a> * Returns this Long divided by the given one.
<a name="line599"></a> * @param {goog.math.Long} other Long by which to divide.
<a name="line600"></a> * @return {!goog.math.Long} This Long divided by the given one.
<a name="line601"></a> */
<a name="line602"></a>goog.math.Long.prototype.div = function(other) {
<a name="line603"></a>  if (other.isZero()) {
<a name="line604"></a>    throw Error(&#39;division by zero&#39;);
<a name="line605"></a>  } else if (this.isZero()) {
<a name="line606"></a>    return goog.math.Long.ZERO;
<a name="line607"></a>  }
<a name="line608"></a>
<a name="line609"></a>  if (this.equals(goog.math.Long.MIN_VALUE)) {
<a name="line610"></a>    if (other.equals(goog.math.Long.ONE) ||
<a name="line611"></a>        other.equals(goog.math.Long.NEG_ONE)) {
<a name="line612"></a>      return goog.math.Long.MIN_VALUE;  // recall that -MIN_VALUE == MIN_VALUE
<a name="line613"></a>    } else if (other.equals(goog.math.Long.MIN_VALUE)) {
<a name="line614"></a>      return goog.math.Long.ONE;
<a name="line615"></a>    } else {
<a name="line616"></a>      // At this point, we have |other| &gt;= 2, so |this/other| &lt; |MIN_VALUE|.
<a name="line617"></a>      var halfThis = this.shiftRight(1);
<a name="line618"></a>      var approx = halfThis.div(other).shiftLeft(1);
<a name="line619"></a>      if (approx.equals(goog.math.Long.ZERO)) {
<a name="line620"></a>        return other.isNegative() ? goog.math.Long.ONE : goog.math.Long.NEG_ONE;
<a name="line621"></a>      } else {
<a name="line622"></a>        var rem = this.subtract(other.multiply(approx));
<a name="line623"></a>        var result = approx.add(rem.div(other));
<a name="line624"></a>        return result;
<a name="line625"></a>      }
<a name="line626"></a>    }
<a name="line627"></a>  } else if (other.equals(goog.math.Long.MIN_VALUE)) {
<a name="line628"></a>    return goog.math.Long.ZERO;
<a name="line629"></a>  }
<a name="line630"></a>
<a name="line631"></a>  if (this.isNegative()) {
<a name="line632"></a>    if (other.isNegative()) {
<a name="line633"></a>      return this.negate().div(other.negate());
<a name="line634"></a>    } else {
<a name="line635"></a>      return this.negate().div(other).negate();
<a name="line636"></a>    }
<a name="line637"></a>  } else if (other.isNegative()) {
<a name="line638"></a>    return this.div(other.negate()).negate();
<a name="line639"></a>  }
<a name="line640"></a>
<a name="line641"></a>  // Repeat the following until the remainder is less than other:  find a
<a name="line642"></a>  // floating-point that approximates remainder / other *from below*, add this
<a name="line643"></a>  // into the result, and subtract it from the remainder.  It is critical that
<a name="line644"></a>  // the approximate value is less than or equal to the real value so that the
<a name="line645"></a>  // remainder never becomes negative.
<a name="line646"></a>  var res = goog.math.Long.ZERO;
<a name="line647"></a>  var rem = this;
<a name="line648"></a>  while (rem.greaterThanOrEqual(other)) {
<a name="line649"></a>    // Approximate the result of division. This may be a little greater or
<a name="line650"></a>    // smaller than the actual value.
<a name="line651"></a>    var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
<a name="line652"></a>
<a name="line653"></a>    // We will tweak the approximate result by changing it in the 48-th digit or
<a name="line654"></a>    // the smallest non-fractional digit, whichever is larger.
<a name="line655"></a>    var log2 = Math.ceil(Math.log(approx) / Math.LN2);
<a name="line656"></a>    var delta = (log2 &lt;= 48) ? 1 : Math.pow(2, log2 - 48);
<a name="line657"></a>
<a name="line658"></a>    // Decrease the approximation until it is smaller than the remainder.  Note
<a name="line659"></a>    // that if it is too large, the product overflows and is negative.
<a name="line660"></a>    var approxRes = goog.math.Long.fromNumber(approx);
<a name="line661"></a>    var approxRem = approxRes.multiply(other);
<a name="line662"></a>    while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
<a name="line663"></a>      approx -= delta;
<a name="line664"></a>      approxRes = goog.math.Long.fromNumber(approx);
<a name="line665"></a>      approxRem = approxRes.multiply(other);
<a name="line666"></a>    }
<a name="line667"></a>
<a name="line668"></a>    // We know the answer can&#39;t be zero... and actually, zero would cause
<a name="line669"></a>    // infinite recursion since we would make no progress.
<a name="line670"></a>    if (approxRes.isZero()) {
<a name="line671"></a>      approxRes = goog.math.Long.ONE;
<a name="line672"></a>    }
<a name="line673"></a>
<a name="line674"></a>    res = res.add(approxRes);
<a name="line675"></a>    rem = rem.subtract(approxRem);
<a name="line676"></a>  }
<a name="line677"></a>  return res;
<a name="line678"></a>};
<a name="line679"></a>
<a name="line680"></a>
<a name="line681"></a>/**
<a name="line682"></a> * Returns this Long modulo the given one.
<a name="line683"></a> * @param {goog.math.Long} other Long by which to mod.
<a name="line684"></a> * @return {!goog.math.Long} This Long modulo the given one.
<a name="line685"></a> */
<a name="line686"></a>goog.math.Long.prototype.modulo = function(other) {
<a name="line687"></a>  return this.subtract(this.div(other).multiply(other));
<a name="line688"></a>};
<a name="line689"></a>
<a name="line690"></a>
<a name="line691"></a>/** @return {!goog.math.Long} The bitwise-NOT of this value. */
<a name="line692"></a>goog.math.Long.prototype.not = function() {
<a name="line693"></a>  return goog.math.Long.fromBits(~this.low_, ~this.high_);
<a name="line694"></a>};
<a name="line695"></a>
<a name="line696"></a>
<a name="line697"></a>/**
<a name="line698"></a> * Returns the bitwise-AND of this Long and the given one.
<a name="line699"></a> * @param {goog.math.Long} other The Long with which to AND.
<a name="line700"></a> * @return {!goog.math.Long} The bitwise-AND of this and the other.
<a name="line701"></a> */
<a name="line702"></a>goog.math.Long.prototype.and = function(other) {
<a name="line703"></a>  return goog.math.Long.fromBits(this.low_ &amp; other.low_,
<a name="line704"></a>                                 this.high_ &amp; other.high_);
<a name="line705"></a>};
<a name="line706"></a>
<a name="line707"></a>
<a name="line708"></a>/**
<a name="line709"></a> * Returns the bitwise-OR of this Long and the given one.
<a name="line710"></a> * @param {goog.math.Long} other The Long with which to OR.
<a name="line711"></a> * @return {!goog.math.Long} The bitwise-OR of this and the other.
<a name="line712"></a> */
<a name="line713"></a>goog.math.Long.prototype.or = function(other) {
<a name="line714"></a>  return goog.math.Long.fromBits(this.low_ | other.low_,
<a name="line715"></a>                                 this.high_ | other.high_);
<a name="line716"></a>};
<a name="line717"></a>
<a name="line718"></a>
<a name="line719"></a>/**
<a name="line720"></a> * Returns the bitwise-XOR of this Long and the given one.
<a name="line721"></a> * @param {goog.math.Long} other The Long with which to XOR.
<a name="line722"></a> * @return {!goog.math.Long} The bitwise-XOR of this and the other.
<a name="line723"></a> */
<a name="line724"></a>goog.math.Long.prototype.xor = function(other) {
<a name="line725"></a>  return goog.math.Long.fromBits(this.low_ ^ other.low_,
<a name="line726"></a>                                 this.high_ ^ other.high_);
<a name="line727"></a>};
<a name="line728"></a>
<a name="line729"></a>
<a name="line730"></a>/**
<a name="line731"></a> * Returns this Long with bits shifted to the left by the given amount.
<a name="line732"></a> * @param {number} numBits The number of bits by which to shift.
<a name="line733"></a> * @return {!goog.math.Long} This shifted to the left by the given amount.
<a name="line734"></a> */
<a name="line735"></a>goog.math.Long.prototype.shiftLeft = function(numBits) {
<a name="line736"></a>  numBits &amp;= 63;
<a name="line737"></a>  if (numBits == 0) {
<a name="line738"></a>    return this;
<a name="line739"></a>  } else {
<a name="line740"></a>    var low = this.low_;
<a name="line741"></a>    if (numBits &lt; 32) {
<a name="line742"></a>      var high = this.high_;
<a name="line743"></a>      return goog.math.Long.fromBits(
<a name="line744"></a>          low &lt;&lt; numBits,
<a name="line745"></a>          (high &lt;&lt; numBits) | (low &gt;&gt;&gt; (32 - numBits)));
<a name="line746"></a>    } else {
<a name="line747"></a>      return goog.math.Long.fromBits(0, low &lt;&lt; (numBits - 32));
<a name="line748"></a>    }
<a name="line749"></a>  }
<a name="line750"></a>};
<a name="line751"></a>
<a name="line752"></a>
<a name="line753"></a>/**
<a name="line754"></a> * Returns this Long with bits shifted to the right by the given amount.
<a name="line755"></a> * @param {number} numBits The number of bits by which to shift.
<a name="line756"></a> * @return {!goog.math.Long} This shifted to the right by the given amount.
<a name="line757"></a> */
<a name="line758"></a>goog.math.Long.prototype.shiftRight = function(numBits) {
<a name="line759"></a>  numBits &amp;= 63;
<a name="line760"></a>  if (numBits == 0) {
<a name="line761"></a>    return this;
<a name="line762"></a>  } else {
<a name="line763"></a>    var high = this.high_;
<a name="line764"></a>    if (numBits &lt; 32) {
<a name="line765"></a>      var low = this.low_;
<a name="line766"></a>      return goog.math.Long.fromBits(
<a name="line767"></a>          (low &gt;&gt;&gt; numBits) | (high &lt;&lt; (32 - numBits)),
<a name="line768"></a>          high &gt;&gt; numBits);
<a name="line769"></a>    } else {
<a name="line770"></a>      return goog.math.Long.fromBits(
<a name="line771"></a>          high &gt;&gt; (numBits - 32),
<a name="line772"></a>          high &gt;= 0 ? 0 : -1);
<a name="line773"></a>    }
<a name="line774"></a>  }
<a name="line775"></a>};
<a name="line776"></a>
<a name="line777"></a>
<a name="line778"></a>/**
<a name="line779"></a> * Returns this Long with bits shifted to the right by the given amount, with
<a name="line780"></a> * the new top bits matching the current sign bit.
<a name="line781"></a> * @param {number} numBits The number of bits by which to shift.
<a name="line782"></a> * @return {!goog.math.Long} This shifted to the right by the given amount, with
<a name="line783"></a> *     zeros placed into the new leading bits.
<a name="line784"></a> */
<a name="line785"></a>goog.math.Long.prototype.shiftRightUnsigned = function(numBits) {
<a name="line786"></a>  numBits &amp;= 63;
<a name="line787"></a>  if (numBits == 0) {
<a name="line788"></a>    return this;
<a name="line789"></a>  } else {
<a name="line790"></a>    var high = this.high_;
<a name="line791"></a>    if (numBits &lt; 32) {
<a name="line792"></a>      var low = this.low_;
<a name="line793"></a>      return goog.math.Long.fromBits(
<a name="line794"></a>          (low &gt;&gt;&gt; numBits) | (high &lt;&lt; (32 - numBits)),
<a name="line795"></a>          high &gt;&gt;&gt; numBits);
<a name="line796"></a>    } else if (numBits == 32) {
<a name="line797"></a>      return goog.math.Long.fromBits(high, 0);
<a name="line798"></a>    } else {
<a name="line799"></a>      return goog.math.Long.fromBits(high &gt;&gt;&gt; (numBits - 32), 0);
<a name="line800"></a>    }
<a name="line801"></a>  }
<a name="line802"></a>};
</pre>


</body>
</html>
